/*
 * Principale.c
 *
 *  Created on: 20 oct. 2011
 *      Author: alex
 */
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#include "Principale.h"
#include "Sudoku.h"

int main( void ){
	/*					CREATION DES VARIABLES 						*/
	Sudoku_s *sudoku = NULL, *memoire = NULL;

	/* 					INITIALISATION DES VARIABLES				*/
	sudoku = creer_sudoku_std( );
	srand( time(NULL) );

	/*					PROGRAMME									*/

	construit_grille3( sudoku );
	memoire = copy_sudoku( sudoku );
#ifdef DEBUG
	printf("Grille rempli:\n");
	print_Sudoku( sudoku );
#endif

	troue_grille( sudoku );
#ifdef DEBUG
	printf("Apres suppression de quelques trous:\n");
#endif
	print_Sudoku( sudoku );

	joue( memoire, sudoku );

	/*					LIBERATION DES VARIABLES					*/
	libere_sudoku( &sudoku );
	libere_sudoku( &memoire );
	return EXIT_SUCCESS;
}

int calcul_nbre_tour( const Sudoku_s const * sudoku ){
	int i,j;

	int nbre_tour = 0;

	for( i = 0; i < sudoku->hauteur; i++ ){
		for( j = 0; j < sudoku->largeur; j++ ){
			if( sudoku->tab[i][j] == 0 )
				nbre_tour++;
		}
	}

	return nbre_tour;
}

void joue ( const Sudoku_s const *sudoku_plein, Sudoku_s *sudoku_vide ) {
	int x,y, proposition;
	int nbreTour = calcul_nbre_tour( sudoku_vide );
	time_t debut, fin;

	debut = time(NULL);
	while( nbreTour != 0 ){
		nbreTour--;
		do{
			printf("x:\t");
			x = demande_lettre()-49;
			while( demande_lettre() != '\n' ){
			}
			printf("y:\t");
			y = demande_lettre()-49;

			while( demande_lettre() != '\n' ){
			}
		}while( x < 0 || x > 8 || y < 0 && y > 8  || sudoku_vide->tab[y][x] != 0);

		do{
			printf("valeur:\t");
			proposition = demande_lettre()-48;

			while( demande_lettre() != '\n' ){
			}
		}while( proposition < 1 || proposition > 9 );

		system("clear");
		if( sudoku_plein->tab[y][x] == proposition ){
			sudoku_vide->tab[y][x] = proposition;
			printf("Exacte!\n");
		}else{
			nbreTour++;
			printf("Fausse proposition!\n");
		}

		print_Sudoku( sudoku_vide );
	}

	fin = time(NULL);
	printf("Temps: %g\n",difftime( fin, debut));
	printf("Felicitation vous avez reussi!\n");
}
